function averages = gen_ca_match_score_wm

pool_length_in_bins = 100;

seed = 0;
averages = single_subject(seed,pool_length_in_bins)
i=2;
for seed = 100:100:400
    averages(:,:,i) = single_subject(seed,pool_length_in_bins);
    i=i+1;
end
pooled_averages = mean(averages,3);

close all

figure(2);

% Plot over the single object plot.
hold off;
plot(pooled_averages(1,:),'y','LineWidth',3);
hold on;
plot(pooled_averages(2,:),'g','LineWidth',3);
plot(pooled_averages(3,:),'r','LineWidth',3);
plot(pooled_averages(4,:),'c','LineWidth',3);

axis([0 pool_length_in_bins 0 1]);

ylabel('Match score','FontSize',16,'Color','w')
xlabel('T (sec)','FontSize',16,'Color','w')

set(gca,'XTick',[1:pool_length_in_bins/5:pool_length_in_bins],'FontSize',14,'XColor',[1 1 1])
set(gca,'XTickLabel',{'0','1','2','3','4','5'},'XColor',[1 1 1])

set(gca,'YTick',0:.2:1,'FontSize',14,'YColor','w')
%legend({'Match response', 'Mismatch'},'FontSize',14);

set(gcf, 'color', 'k');
set(gca, 'color', 'k');
set(gcf, 'InvertHardCopy', 'off');

h=legend('Stimulus A at theta deg.','Stim. at theta + 90 deg.','Stim. at theta + 180 deg.','Stim. at theta + 270 deg.');
set(h,'Color','w');




end

%% Average match scores over 1 subject's trials.
%Read data from the pwd.
function subject1_pooled_averages = single_subject(seed, pool_length_in_bins)

filename = dir(['final_seed' num2str(seed,'%03d') '_phase_6*']);
cd (filename.name);

analyzeSequenceEssential('WM','p23',500,999,152000,155999,905000,1224999)
global PSTATES

% Reconstruct the start bins for the Mental rotation experiment: 64 trials
% of 5 seconds starting at the beginning of the trial.
startbin=0*20+1;trial_bins = reshape(startbin:100:64*100,8,8)';

stimulus_numbers = repmat([1:4]',1,4)';
object1_pool_start_bins = [];
object1_object_to_pool = [];
for position = 1:4
    temp = trial_bins(1:4,1:4)';
    object1_pool_start_bins(position,:) = temp(:)';

    object1_object_to_pool(position,:) = stimulus_numbers(:)';
    stimulus_numbers = [stimulus_numbers(:,2:end) stimulus_numbers(:,1)];
    
end

object1_pooled_averages = plot_georgopoulos(PSTATES, object1_pool_start_bins,...
    object1_object_to_pool , pool_length_in_bins);


% Analyze the other object now; template is taken from different window
% containing object2.
analyzeSequenceEssential('WM','p23',500, 999, 156000, 159999, 905000,1224999)
global PSTATES

stimulus_numbers = repmat([1:4]',1,4)';
object2_pool_start_bins = [];
object2_object_to_pool = [];
for position = 1:4
    temp = trial_bins(5:8,5:8)';
    object2_pool_start_bins(position,:) = temp(:)';

    object2_object_to_pool(position,:) = stimulus_numbers(:)';
    stimulus_numbers = [stimulus_numbers(:,2:end) stimulus_numbers(:,1)];
    
end
object2_pooled_averages = plot_georgopoulos(PSTATES, object2_pool_start_bins,...
    object2_object_to_pool , pool_length_in_bins);


subject1_pooled_averages = (object1_pooled_averages + object2_pooled_averages)*0.5;

close all

figure(2);

% Plot over the single object plot.
hold off;
plot(subject1_pooled_averages(1,:),'y','LineWidth',3);
hold on;
plot(subject1_pooled_averages(2,:),'g','LineWidth',3);
plot(subject1_pooled_averages(3,:),'r','LineWidth',3);
plot(subject1_pooled_averages(4,:),'c','LineWidth',3);

ylabel('Match score','FontSize',16,'Color','w')
xlabel('T (msec)','FontSize',16,'Color','w')

set(gca,'XTick',[1:pool_length_in_bins/5:pool_length_in_bins],'FontSize',14,'XColor',[1 1 1])
set(gca,'XTickLabel',{'0','1000','2000','3000','4000','5000'},'XColor',[1 1 1])

set(gca,'YTick',0:.2:1,'FontSize',14,'YColor','w')
%legend({'Match response', 'Mismatch'},'FontSize',14);

set(gcf, 'color', 'k');
set(gca, 'color', 'k');
set(gcf, 'InvertHardCopy', 'off');

h=legend('Stimulus A at theta deg.','Stim. at theta + 90 deg.','Stim. at theta + 180 deg.','Stim. at theta + 270 deg.');
set(h,'Color','w');

cd ..


end

